package com.aier.user.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.common.common.CommonSession;
import com.aier.common.common.Constant;
import com.aier.common.common.ICommonDAO;
import com.aier.user.entity.User;
import com.aier.user.entity.userapp.Bill;
import com.aier.user.entity.userapp.Commidity;
import com.aier.user.entity.userapp.UserModule;
import com.aier.user.service.IFindPeopleService;
import com.aier.user.service.IUserBuyService;

@Scope("prototype")
@Component("userPayPlatformAction")
public class UserPayPlatformAction extends UserBaseAction {

    /**
     * 支付
     */
    private static final long serialVersionUID = 1L;


    @Resource(name = "userBuyService")
    private IUserBuyService userBuyService;
    @Resource(name = "findPeopleService")
    private IFindPeopleService findPeopleService;

    
    private int s;                    // 错误码
    private String error_info;        // 错误信息
    private int page; // 页数
    private int page_size; // 每页的记录数
    private int page_count; // 总页数
    private int count; // 总数
    
    private Long commidity_id;        // 商品id
    private Long bill_id;            // 订单id
    private Bill bill;                // 订单对象
    
    private List<Commidity> commidityList;    // 商品列表
    private List<Bill> billList;            // 账单列表
    
    private int com_num;            // 商品数量
    private String currentDate;        // 当前日期
    private Date edate;                // 用户的家校练习薄有效截止日期
    
    private Long buid; // 学生用户id
    private String bname; // 宝宝名字
    private Long cid; // 班级id
    private String paccount;//用户账号
    private String validity;//有效期限日
    
    private User userInfo; // 宝宝信息
    @Override
    public String execute() {
        Long uid = (Long) session.get(CommonSession.SESSION_ACCOUNT_ID);
        paccount = (String) session.get(CommonSession.SESSION_ACCOUNT);
        Date date = new Date();
        currentDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
        UserModule usermodule = userBuyService.searchUserModule(uid,1L);
        if(usermodule!=null){
            edate = usermodule.getEdate();
        }else{
            edate = null;
        }
        if(bill_id!=null){
            bill = userBuyService.searchBillById(bill_id);
        }
        
        userInfo = findPeopleService.searchSelfInfo(buid);
        
        return SUCCESS;
    }

    /**
     * 查看购买记录
     * @return
     */
    @SuppressWarnings("unchecked")
    public String ShowBillList(){
        Long uid = (Long) session.get(CommonSession.SESSION_ACCOUNT_ID);
        s = 0;
        if (page <= 0) {
            page = 1;
        }
        try {
            Map<String, Object> map = userBuyService.searchUserBillList(page,Constant.num_10, uid);
            if (null != map) {
                billList = (List<Bill>) map.get(ICommonDAO.OBJ_LIST);
                if (null != billList) {
                    s = 1;
                }
                page_count = (Integer) map.get(ICommonDAO.PAGE_COUNT);
                count = (Integer) map.get(ICommonDAO.COUNT);
                page = (Integer) map.get(ICommonDAO.PAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }
    
    /**
     * 显示商品列表
     * @return
     */
    public String ShowCommidityList(){
        commidityList = userBuyService.searchCommidityList(1);
        if(commidityList==null){
            s = 0;
        }else{
            s = 1;
        }
        return SUCCESS;
    }
    
    
    /**
     * 生成订单
     * @return
     */
    public String createBill(){
        Long uid = (Long) session.get(CommonSession.SESSION_ACCOUNT_ID);
        bill = userBuyService.insertBill(uid, commidity_id,com_num);
        return SUCCESS;
    }
    

    /**
     * 删除订单
     * @return
     */
    public String deleteBill(){
        userBuyService.deleteBill(bill_id);
        return SUCCESS;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getPage_size() {
        return page_size;
    }

    public void setPage_size(int page_size) {
        this.page_size = page_size;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public int getS() {
        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public int getPage_count() {
        return page_count;
    }

    public Bill getBill() {
        return bill;
    }

    public List<Commidity> getCommidityList() {
        return commidityList;
    }

    public List<Bill> getBillList() {
        return billList;
    }

    public void setUserBuyService(IUserBuyService userBuyService) {
        this.userBuyService = userBuyService;
    }

    public void setCommidity_id(Long commidity_id) {
        this.commidity_id = commidity_id;
    }

    public void setBill_id(Long bill_id) {
        this.bill_id = bill_id;
    }

    public void setCom_num(int com_num) {
        this.com_num = com_num;
    }


    public Date getEdate() {
        return edate;
    }

    public Long getBuid() {
        return buid;
    }

    public void setBuid(Long buid) {
        this.buid = buid;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public Long getCid() {
        return cid;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public String getPaccount() {
        return paccount;
    }

    public Long getBill_id() {
        return bill_id;
    }

    public String getValidity() {
        return validity;
    }

    public void setValidity(String validity) {
        this.validity = validity;
    }

    public String getCurrentDate() {
        return currentDate;
    }

    public User getUserInfo() {
        return userInfo;
    }

    public void setFindPeopleService(IFindPeopleService findPeopleService) {
        this.findPeopleService = findPeopleService;
    }
    
}
